package com.example.springbootthread.perpetualmotion;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean;

import java.util.Map;
import java.util.concurrent.*;

/**
 * @author : guorf
 * @version : v1.0
 * @description : TODO 线程池工具类
 * @createTime : 2022/10/12 11:32
 */
public class TaskProcessUtil {
    // 每个任务都有自己单独的线程池
    private static Map<String, ExecutorService> executors = new ConcurrentHashMap<>();

    // 初始化一个线程池
    private static ExecutorService init(String poolName, int poolSize) {
        return new ThreadPoolExecutor(poolSize, poolSize,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(),
                new ThreadFactoryBuilder().setNamePrefix("Pool-" + poolName).setDaemon(false).build(),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }
    // 获取线程池
    public static ExecutorService getOrInitExecutor(String poolName, int poolSize) {
        ExecutorService executorService = executors.get(poolName);
        if (null == executorService) {
            synchronized (TaskProcessUtil.class){
                executorService = executors.get(poolName);
                if (null == executorService) {
                    executorService = init(poolName, poolSize);
                    executors.put(poolName, executorService);
                }
            }

        }
        return executorService;
    }

    // 回收线程资源
    public static void releaseExecutors(String poolName){
        ExecutorService executorService = executors.get(poolName);
        if (executorService != null) {
            executorService.shutdown();
        }
    }
}
